package com.cskaoyan.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;

/**
 * 创建日期: 2022/05/19 11:40
 *
 * @author ciggar
 */
public class MybatisUtils {

    static SqlSessionFactory sqlSessionFactory;

    static {

        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        try {
            sqlSessionFactory = sqlSessionFactoryBuilder.build(Resources.getResourceAsStream("mybatis-config.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    // 获取 sqlSession的方法
    public static SqlSession getSqlSession(){

        SqlSession sqlSession = sqlSessionFactory.openSession();

//        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        return sqlSession;

    }
}
